#include "kernel_operator.h"

constexpr int32_t TOTAL_LENGTH = 8 * 2048;
constexpr int32_t USE_CORE_NUM = 8;
constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM;
constexpr int32_t TILE_NUM = 8;
constexpr int32_t BUFFER_NUM = 2;
constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM;

template <typename T>
class KernelMaskedFill {
public:
    __aicore__ inline KernelMaskedFill() {}

    __aicore__ inline void Init(GM_ADDR x, GM_ADDR mask, GM_ADDR valueT, GM_ADDR z)
    {
        const T maskValue = *reinterpret_cast<const __gm__ T*>(valueT);
        this->value = maskValue;
        xGm.SetGlobalBuffer((__gm__ T *)x + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        maskGm.SetGlobalBuffer((__gm__ bool *)mask + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        zGm.SetGlobalBuffer((__gm__ T *)z + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(T));
        pipe.InitBuffer(inQueueM, BUFFER_NUM, TILE_LENGTH * sizeof(bool));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, TILE_LENGTH * sizeof(T));
    }

    __aicore__ inline void Process()
    {
        int32_t loopCount = TILE_NUM * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<T> xLocal = inQueueX.AllocTensor<T>();
        AscendC::LocalTensor<bool> maskLocal = inQueueM.AllocTensor<bool>();
        AscendC::DataCopy(xLocal, xGm[progress * TILE_LENGTH], TILE_LENGTH);
        AscendC::DataCopy(maskLocal, maskGm[progress * TILE_LENGTH], TILE_LENGTH);
        inQueueX.EnQue(xLocal);
        inQueueM.EnQue(maskLocal);
    }

    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<T> xLocal = inQueueX.DeQue<T>();
        AscendC::LocalTensor<bool> maskLocal = inQueueM.DeQue<bool>();
        AscendC::LocalTensor<T> zLocal = outQueueZ.AllocTensor<T>();

        for(int i = 0; i < TILE_LENGTH; i++){
            if(maskLocal.GetValue(i) == true){
                zLocal.SetValue(i, this->value);
            }
            else{
                zLocal.SetValue(i, xLocal.GetValue(i));
            }
        }

        outQueueZ.EnQue<T>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueM.FreeTensor(maskLocal);
    }

    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<T> zLocal = outQueueZ.DeQue<T>();
        AscendC::DataCopy(zGm[progress * TILE_LENGTH], zLocal, TILE_LENGTH);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX, inQueueM;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<T> xGm;
    AscendC::GlobalTensor<bool> maskGm;
    AscendC::GlobalTensor<T> zGm;
    T value;
};

extern "C" __global__ __aicore__ void masked_fill_custom(GM_ADDR x, 
                                                         GM_ADDR mask, 
                                                         GM_ADDR value, 
                                                         GM_ADDR z)
{ 
    KernelMaskedFill<half> op;
    op.Init(x, mask, value, z);
    op.Process();
}

#ifndef ASCENDC_CPU_DEBUG
void masked_fill_custom_do(uint32_t blockDim, 
                           void *stream, 
                           uint8_t *x, 
                           uint8_t *mask, 
                           uint8_t *value, 
                           uint8_t *z)
{
    masked_fill_custom<<<blockDim, nullptr, stream>>>(x, mask, value, z);
}
#endif
